$(function () {
  let $btn = $('input[type="button"]')
  let code = new Behave({
    textarea: $("#main textarea").get(0),
    replaceTab: true,
    softTabs: true,
    tabSize: 2,
    autoOpen: true,
    overwrite: true,
    autoStrip: true,
    autoIndent: true,
    fence: false
  })
  $btn.click(() => {
    let wuhu = $('<div><pre class="javascript"></pre><i class="iconfont icon-clipboard" title="复制到剪贴板"></i><i class="iconfont icon-delete" title="删除"></i></div>');
    code = $("#main textarea");

    if (code.val() !== "") {
      wuhu.find("pre").html(code.val());
      hljs.highlightBlock(wuhu.find("pre").get(0));
      $("#main").append(wuhu)
    }

    let del = wuhu.find("i.icon-delete");
    let cp = wuhu.find("i.icon-clipboard");
    del.css({
      position: 'absolute',
      top:'2px',
      right:'2px',
      color:"white"
    })
    cp.css({
      position: 'absolute',
      top:'2px',
      right:'25px',
      color:"white"
    })
    del.click(() => {
      wuhu.remove()
    })
    cp.click(() => {
      let e = wuhu.find("pre");
      icon = document.createRange();
      icon.selectNode(e[0]);
      window.getSelection().addRange(icon);
      document.execCommand("copy");
      window.getSelection().removeAllRanges()
    })
  }), code
});
